package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.angency.AgencyScopeDto;
import com.itheima.pinda.entity.agency.PdAgencyScope;
import com.itheima.pinda.mapper.PdAgencyScopeMapper;
import com.itheima.pinda.mapstruct.AgencyDTO2Entity;
import com.itheima.pinda.service.PdAgencyScopeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PdAgencyScopeServiceImpl extends ServiceImpl<PdAgencyScopeMapper, PdAgencyScope> implements PdAgencyScopeService {

    @Resource
    private AgencyDTO2Entity agencyDTO2Entity;

    /**
     * 批量保存机构业务范围
     *
     * @param dtoList 机构业务范围信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveAgencyScope(List<AgencyScopeDto> dtoList) {
        List<PdAgencyScope> pdAgencyScopeList = dtoList.stream()
                .map(dto -> agencyDTO2Entity.conver(dto))
                .collect(Collectors.toList());
        return super.saveBatch(pdAgencyScopeList);
    }

    /**
     * 删除机构业务范围信息
     *
     * @param dto 参数
     * @return 是否成功
     */
    @Override
    public boolean deleteAgencyScope(AgencyScopeDto dto) {
        PdAgencyScope pdAgencyScope = agencyDTO2Entity.conver(dto);

        // todo 优化一下 如果两个条件都存在, 就不进行删除操作 | 以免全表删除
        // 解决方式

        LambdaQueryWrapper<PdAgencyScope> lambdaQuery = Wrappers.lambdaQuery();
        boolean canExecute = false;
        if (StringUtils.isNotBlank(pdAgencyScope.getAgencyId())) {
            lambdaQuery.eq(PdAgencyScope::getAgencyId, pdAgencyScope.getAgencyId());
            canExecute = true;
        }
        if (StringUtils.isNotBlank(pdAgencyScope.getAreaId())) {
            lambdaQuery.eq(PdAgencyScope::getAreaId, pdAgencyScope.getAreaId());
            canExecute = true;
        }

        // 最初的if判断表达式
        // if (canExecute) {
        //     return super.remove(lambdaQuery);
        // } else {
        //     return false;
        // }

        // 自己优化的三元表达式
        // return canExecute ? super.remove(lambdaQuery) : false;
        // 编译器对三元表达式进一步优化
        return canExecute && super.remove(lambdaQuery);
    }


    /**
     * 获取机构业务范围列表
     *
     * @param areaId   行政区域id
     * @param agencyId 机构id
     * @return 机构业务范围列表
     */
    @Override
    public List<AgencyScopeDto> findAll(String areaId, String agencyId, List<String> agencyIds, List<String> areaIds) {
        List<PdAgencyScope> pdAgencyScopeList = super.list(Wrappers.<PdAgencyScope>lambdaQuery()
                .eq(StringUtils.isNotBlank(areaId), PdAgencyScope::getAreaId, areaId)
                .eq(StringUtils.isNotBlank(agencyId), PdAgencyScope::getAgencyId, agencyId)
                .in(areaIds != null && areaIds.size() > 0, PdAgencyScope::getAreaId, areaIds)
                .in(agencyIds != null && agencyIds.size() > 0, PdAgencyScope::getAgencyId, agencyIds));

        return pdAgencyScopeList.stream()
                .map(item -> agencyDTO2Entity.conver(item))
                .collect(Collectors.toList());
    }
}
